<?php 
/*
Template Name: Archives
*/
?>

<?php

function get_the_archives() {
	global $wpdb, $wp_locale;

	// Query string
	$qstring = "SELECT DISTINCT YEAR(post_date) AS `year`,";
	$qstring .= " MONTH(post_date) AS `month`,";
	$qstring .= " count(ID) AS posts FROM  $wpdb->posts";
	$qstring .= " WHERE post_type = 'post' AND post_status = 'publish'";
	$qstring .= " GROUP BY YEAR(post_date), MONTH(post_date)";
	$qstring .= " ORDER BY YEAR(post_date) DESC, MONTH(post_date) ASC";

	// Query database
	$arcresults = $wpdb->get_results($qstring);
	
	$currentyear = '';
	if ( $arcresults ) {
		$output = '<ul id="archives">';
		foreach ( (array) $arcresults as $arcresult ) {
			if ($currentyear != $arcresult->year) {
				if ('' != $currentyear) {
					$output .= '</ul></li>';
				}
				$url = get_year_link( $arcresult->year );
				//$output .= '<li>' . $arcresult->year . '<ul>';
				$output .= '<li>' . '<a href="' . $url . '">' . $arcresult->year . '</a>' . '<ul>';
				$currentyear = $arcresult->year;
			}
			$url = get_month_link( $arcresult->year, $arcresult->month );
			$monthname = $wp_locale->get_month($arcresult->month);
			//$text = $monthname . '&nbsp;('.$arcresult->posts.')';
			//$output .= get_archives_link($url, $text, 'html', '', '');
			$output .= get_archives_link($url, $monthname, 'html', '', '&nbsp;('.$arcresult->posts.')');
		}
		$output .= '</ul></li>';
		$output .= '</ul>';
	} else {
		$output = "<p>Aucun article.</p>";
	}
	print $output;
}

?>

<?php get_header(); ?>

	<div class="post" id="search">
		<div class="post-header">
			<h3 class="post-title">Recherche</h3>
		</div><!-- End div post-header -->
		<div class="post-content">
		<form method="get" id="searchform" action="/index.php"
		  name="searchform">
			<p style="margin: 5px" align="center">
			  <input type="text" name="s" value=""
			  id="searchbox" />
			  <input type="submit" value="Recherche" />
			</p>
		</form>
		</div><!-- End div post-content -->
	</div><!-- End div post -->

	<div class="post" id="search">
		<div class="post-header">
			<h3 class="post-title">Archives par cat&eacute;gories</h3>
		</div><!-- End div post-header -->
		<div class="post-content">
		    <?php wp_list_categories('show_count=1&title_li='); ?>
		</div><!-- End div post-content -->
	</div><!-- End div post -->

	<div class="post" id="search">
		<div class="post-header">
			<h3 class="post-title">Archives par mots-cl&eacute;s</h3>
		</div><!-- End div post-header -->
		<div class="post-content">
		    <?php wp_tag_cloud(); ?>
		</div><!-- End div post-content -->
	</div><!-- End div post -->

	<div class="post" id="search">
		<div class="post-header">
			<h3 class="post-title">Archives par mois&nbsp;:</h3>
		</div><!-- End div post-header -->
		<div class="post-content">
		    <?php get_the_archives(); ?>
		</div><!-- End div post-content -->
	</div><!-- End div post -->

<?php get_footer(); ?>
